package com.sibu.yx.promotion.admin.web.controller.excel;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

import javax.servlet.http.HttpServletResponse;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.excel.EasyExcel;
import com.sibu.yx.promotion.persistence.request.ImportDemoRequest;
import com.sibu.yx.promotion.service.impl.excel.ImportExcelEventDemoListener;
import com.sibu.yx.promotion.persistence.vo.ExportInfoVo;

import lombok.extern.slf4j.Slf4j;

/**
 * @author suzheng
 * @date 2020-06-17
 **/
@Slf4j
@Controller
@RequestMapping(value = "/import", produces = MediaType.APPLICATION_JSON_VALUE)
public class ExcelImportController extends AbstractExportBaseController {

    @PostMapping("/demo")
    public void upload(HttpServletResponse response, MultipartFile file) throws IOException {
        if (file == null) {
            super.returnErrExportResponse(response, "文件不能为空");
        }
        ImportExcelEventDemoListener uploadOrderExpressInfoListener =
                new ImportExcelEventDemoListener();
        EasyExcel.read(Objects.requireNonNull(file).getInputStream(), ImportDemoRequest.class, uploadOrderExpressInfoListener).sheet().headRowNumber(1)
                .doRead();
        List<ImportDemoRequest> expressInfoListenerFailResultList = uploadOrderExpressInfoListener.getFailResultList();
        ExportInfoVo<ImportDemoRequest> exportInfoVo = new ExportInfoVo<>();
        exportInfoVo.setDataList(expressInfoListenerFailResultList);
        exportInfoVo.setOutFileName("导入结果");
        exportInfoVo.setDataClass(ImportDemoRequest.class);
        super.returnExportResponse(response, exportInfoVo);
    }

}
